﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CoreDC.Classes;

namespace CoreDC.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new RunClient();
            client.Run();
        }
    }

    public class RunClient : Interfaces.IChat, Interfaces.IRaw
    {
        bool runGlobal = true;
        
        public void Run()
        {
            using (var myDcClient = new DcClient())
            {
                myDcClient.OnMainchatMessage += new DcUserMessageHandler(DcClient_OnMainchatMessage);
                myDcClient.OnPrivateMessageRecieved +=new DcPrivateMessageHandler(DcClient_OnPrivateMessageRecieved);
                myDcClient.OnPrivateMessageSent += new DcPrivateMessageHandler(DcClient_OnPrivateMessageSent);
                myDcClient.OnMessageReceived += new DcMessageHandler(DcClient_OnMessageReceived);
                myDcClient.OnMessageSent +=new DcMessageHandler(DcClient_OnMessageSent);
                
                myDcClient.ClientInfo.Description = "<++ V:0.699,M:A,H:1/0/1,S:7>";
                myDcClient.ClientInfo.Share = CoreDC.Classes.ClientInfo.Sharesize.Small;

                ShowWelcome();
                while (runGlobal)
                {
                    var input = Console.ReadLine();
                    RunCommand(input, myDcClient);
                }
            }
        }

        void RunCommand(string cmd, DcClient myDcClient)
        {
            if (cmd.Length > 3)
            {
                var command = cmd.Substring(0, 3);
                var variables = cmd.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                switch (command)
                {
                    case "/he":
                        ShowHelp();
                        break;
                    case "/se":
                        myDcClient.ClientInfo.Username = variables[1];
                        Console.WriteLine("Username set.");
                        if (variables.Length > 2)
                        {
                            myDcClient.ClientInfo.Password = variables[2];
                            Console.WriteLine("Password set.");
                        }
                        break;
                    case "/jo":
                        if (variables.Length > 1)
                        {
                            myDcClient.ClientInfo.HubAdress = variables[1];
                            myDcClient.Connect();
                        }
                        break;
                    case "/ex":
                        myDcClient.Disconnect();
                        break;
                    case "/li":
                        if (variables.Length > 1)
                        {
                            var list = myDcClient.NickList.Find(variables[1]);
                            foreach (var user in list)
                            {
                                Console.WriteLine(user.Username);
                            }
                        }
                        else
                        {
                            foreach (var user in myDcClient.NickList)
                            {
                                Console.WriteLine(user.Username);
                            }
                        }                            
                        break;
                    case "/ch":
                        myDcClient.SendMainchatMessage(cmd.Substring(6));
                        break;
                    case "/pm":
                        var to = variables[1];
                        var message = cmd.Substring(cmd.IndexOf(to) + to.Length + 1);
                        myDcClient.SendPrivateMessage(to, message);
                        break;
                    case "/cl":
                        runGlobal = false;
                        break;
                    default:
                        break;
                }
            }
        }

        void ShowHelp()
        {
            var help = new StringBuilder();

            help.Append("---------" + Environment.NewLine);
            help.Append("Available commands:" + Environment.NewLine);
            help.Append("/help - show this message" + Environment.NewLine);
            help.Append("/set <username> [password] - set username and password (optional)" + Environment.NewLine);
            help.Append("/join host:port - joins a hub" + Environment.NewLine);
            help.Append("/exit - exits hub" + Environment.NewLine);
            help.Append("/list [username] - list users. Ex. /list (shows all), /list todi (shows all usernames containing 'todi')" + Environment.NewLine);
            help.Append("/chat <message> - sends <message> to mainchat of connected hub" + Environment.NewLine);
            help.Append("/pm <user> <message> - open pm chat with user" + Environment.NewLine);
            help.Append("/close - closes this client" + Environment.NewLine);
            help.Append("---------");

            Console.WriteLine(help.ToString());
        }

        void ShowWelcome()
        {
            Console.WriteLine("Welcome to the CoreDC ConsoleDemo. Type /help to get available commands");
        }

        #region IChat Members

        public void DcClient_OnMainchatMessage(DcClient sender, User user, string text, bool ownMessage)
        {
            Console.WriteLine("[mainchat] <" + user.Username + "> " + text);
        }

        public void DcClient_OnPrivateMessageRecieved(DcClient sender, User user, string text)
        {
            Console.WriteLine("[pm -> me] <" + user.Username + "> " + text);
        }

        public void DcClient_OnPrivateMessageSent(DcClient sender, User user, string text)
        {
            Console.WriteLine("[pm -> " + user.Username + "] <" + user.Username + "> " + text);
        }

        #endregion


        #region IRaw Members

        public void DcClient_OnMessageReceived(DcClient sender, string text)
        {
            Console.WriteLine(text);
        }

        public void DcClient_OnMessageSent(DcClient sender, string text)
        {
            Console.WriteLine(text);
        }

        public void DcClient_OnTrace(DcClient sender, string text)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
